﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using TomShane.Neoforce.Controls;

namespace ClickGame.GUI.Controls
{
    public class ImageButton : Button
    {
        private Texture2D _image;
        private int width;
        private int height;

        public Texture2D Image
        {
            get { return _image; }
            set
            {
                _image = value;
                int height = Height-5;
                int width = Width;
                float ratio = 0.0f;
                int orig_w = _image.Width;
                int orig_h = _image.Height;

                if (orig_w > orig_h) //find the greater proportion
                {
                    ratio = (float)Width / (float)orig_w;
                    height = (int)((float)orig_h * ratio);
                }
                else
                {
                    ratio = (float)Height / (float)orig_h;
                    width = (int)((float)orig_w * ratio);
                }


                CalculatedPaddingLeft = width;
            }
        }
        public ImageButton(Manager manager)
            : base(manager)
        {

        }

        protected override void DrawControl(Renderer renderer, Rectangle rect, GameTime gameTime)
        {
            Rectangle r = new Rectangle(rect.X+2,rect.Y+2,rect.Width,rect.Height-6);
            int height = r.Height;
            int width = r.Width;
            float ratio = 0.0f;
            int orig_w = _image.Width;
            int orig_h = _image.Height;

            if (orig_w > orig_h) //find the greater proportion
            {
                ratio = (float)r.Width / (float)orig_w;
                height = (int)((float)orig_h * ratio);
            }
            else
            {
                ratio = (float)r.Height / (float)orig_h;
                width = (int)((float)orig_w * ratio);
            }


            CalculatedPaddingLeft = width;
            base.DrawControl(renderer, rect, gameTime);
            if (ControlState != ControlState.Pressed)
            {
                if(Enabled)
                    renderer.Draw(_image, new Rectangle(r.X, r.Y, width, height), Color.White);
                else
                {
                    renderer.Draw(_image, new Rectangle(r.X, r.Y, width, height), Color.LightGray);
                }
            }
            else
            {
                renderer.Draw(_image, new Rectangle(r.X+1, r.Y+1, width, height), Color.White);
            }
        }
    }
}
